package com.winit.openapi.wop.inbound.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wop.inbound.constant.WHInboundConstant;
import com.winit.ums.spi.EorIorInfoService;
import com.winit.ums.spi.commnad.EorIorInfoCommand;
import com.winit.ums.spi.vo.EorIorInfo;
import com.winit.ups.spi.systemrule.CountryService;
import com.winit.ups.spi.systemrule.command.CountryCodeCommand;
import com.winit.ups.spi.systemrule.vo.Country;

@Controller
@RequestMapping(value = "/winit/ums")
public class WHInboundVendorController extends BaseController {

    @Resource
    private EorIorInfoService eorIorInfoService;
    
    @Resource
    private CountryService             countryService;

    /**
     * 根据类型查询进口商/出口商信息列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/getVendorInfo")
    public String getVendorInfo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object params = requestMsg.getData();
        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
            // type不能为空
            ValidateUtil.validNotNull(json, "vendorType");
            ValidateUtil.validNotNull(json, "countryCode");
            ValidateUtil.validMaxLength(json, "vendorType", 50, true);
            ValidateUtil.validMaxLength(json, "countryCode", 50, true);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        String type = json.getString("vendorType");
        if(!WHInboundConstant.VendorType.isValidVendorType(type)){
            throw new ApiException(ErrorCode.FEILD_NOT_MATCH, "vendorType");
        }
        
        String countryCode = json.getString("countryCode");

        CountryCodeCommand codeCommand = new CountryCodeCommand();
        codeCommand.setCountryCode(countryCode);
        Country country = countryService.getCountryByCode(codeCommand);
        if (null == country) {
            throw new ApiException(ErrorCode.FEILD_NOT_MATCH, countryCode);
        }
        // 查询进口商/出口商
        EorIorInfoCommand command = new EorIorInfoCommand();
        command.setCtx(CommandContext.getContext());
        command.setUserId(CommandContext.getContext().getUserId());
        command.setType(type);
        command.setCountryCode(countryCode);
        List<EorIorInfo> infoList = eorIorInfoService.queryEorIorInfoByType(command);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        if(CollectionUtils.isNotEmpty(infoList)){
            for (EorIorInfo info : infoList) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("isWinit", info.getIsWinit());
                map.put("vendorCode", info.getCode());
                //经营单位/进口商，出口商名称
                map.put("vendorName", info.getOperationUnit());
                list.add(map);
            }
        }
        responseMsg.setData(list);
        
        return SUCCESS;
    }

}
